package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzLcMapper;
import com.ruoyi.system.domain.DzLc;
import com.ruoyi.system.service.IDzLcService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 发展里程Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-17
 */
@Service
public class DzLcServiceImpl implements IDzLcService 
{
    @Autowired
    private DzLcMapper dzLcMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询发展里程
     * 
     * @param lcId 发展里程主键
     * @return 发展里程
     */
    @Override
    public DzLc selectDzLcByLcId(Long lcId)
    {
        return dzLcMapper.selectDzLcByLcId(lcId);
    }

    /**
     * 查询发展里程列表
     * 
     * @param dzLc 发展里程
     * @return 发展里程
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<DzLc> selectDzLcList(DzLc dzLc)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzLc.setDatascope(datascope);
        if(datascope==4){
            dzLc.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzLc.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzLc.setCheckHuiyuanId(0l);
            }

        }
        return dzLcMapper.selectDzLcList(dzLc);
    }

    @Override
    public List<DzLc> selectDzLcListWeb(DzLc dzLc)
    {
        return dzLcMapper.selectDzLcList(dzLc);
    }

    /**
     * 新增发展里程
     * 
     * @param dzLc 发展里程
     * @return 结果
     */
    @Override
    public int insertDzLc(DzLc dzLc)
    {
        return dzLcMapper.insertDzLc(dzLc);
    }

    /**
     * 修改发展里程
     * 
     * @param dzLc 发展里程
     * @return 结果
     */
    @Override
    public int updateDzLc(DzLc dzLc)
    {
        return dzLcMapper.updateDzLc(dzLc);
    }

    /**
     * 批量删除发展里程
     * 
     * @param lcIds 需要删除的发展里程主键
     * @return 结果
     */
    @Override
    public int deleteDzLcByLcIds(Long[] lcIds)
    {
        return dzLcMapper.deleteDzLcByLcIds(lcIds);
    }

    /**
     * 删除发展里程信息
     * 
     * @param lcId 发展里程主键
     * @return 结果
     */
    @Override
    public int deleteDzLcByLcId(Long lcId)
    {
        return dzLcMapper.deleteDzLcByLcId(lcId);
    }
}
